<!DOCTYPE html>
<!--
Copyright 2011 WebDriver committers
Copyright 2011 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
  <title>execute_script_test.html</title>
  <script src="../../test_bootstrap.js"></script>
  <script src="../../../atoms/inject/deps.js"></script>
  <script type="text/javascript">
    goog.require('goog.userAgent');
    goog.require('goog.userAgent.product');
    goog.require("goog.testing.jsunit");
    goog.require("webdriver.atoms.inject");
    goog.require("webdriver.atoms.inject.frame");
  </script>

  <script type="text/javascript">
    function isTestSkipped() {
      return goog.userAgent.IE || goog.userAgent.product.SAFARI;
    }

    function testExecuteScriptWithWindow() {
      if (isTestSkipped()) {
        return;
      }
      var func = function () {return document.getElementById('bou').value;};
      var win = getValue(webdriver.atoms.inject.frame.findFrameByIdOrName("iframe"));
      assertEquals("there", getValue(webdriver.atoms.inject.executeScript(func, [], win)));
    }

    function testExecuteScriptWithStringFunctionWithWindow() {
      if (isTestSkipped()) {
        return;
      }
      var func = "return document.getElementById('bou').value";
      var win = getValue(webdriver.atoms.inject.frame.findFrameByIdOrName("iframe"));
      assertEquals("there", getValue(webdriver.atoms.inject.executeScript(func, [], win)));
    }

    function testExecuteScriptWithNullWindow() {
      var func = function () {return document.getElementById('bou').value;};
      assertEquals("here", getValue(webdriver.atoms.inject.executeScript(func, [], null)));
    }

    function testExecuteScriptWithoutWindow() {
      var func = function () {return document.getElementById('bou').value;};
      assertEquals("here", getValue(webdriver.atoms.inject.executeScript(func, [])));
    }

    function testCanExecuteScriptsThatReturnPrimitives() {
      var execute = webdriver.atoms.inject.executeScript;
      assertEquals(2, getValue(execute('return 1 + 1;', [])));
      assertEquals('hello', getValue(execute('return "hello";', [])));
      assertTrue(getValue(execute('return true;', [])));
    }

    function testCanReturnElements() {
      var execute = webdriver.atoms.inject.executeScript;
      var webElement = getValue(execute('return document.body', []));
      assertEquals(document.body, bot.inject.cache.getElement(webElement['ELEMENT']));
    }

    function getValue(rawResult) {
      return goog.json.parse(rawResult).value;
    }
  </script>
</head>
<body>
  <p id="x" name="para">Para</p>
  <input type="text" id="bou" value="here"/>
  <iframe src="nested_iframes.html" id="iframe"></iframe>
  </body>
</html>
